<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form ref="userInfoRef" :model="userInfo" :rules="userInfoRules" label-width="120px" style="margin-left: 120px; margin-top: 30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width: 300px" />
              </el-form-item>
              <el-form-item label="新密码:" prop="password">
                <el-input v-model="userInfo.password" style="width: 300px" type="password" />
              </el-form-item>
              <el-form-item label="确认新密码:" prop="repassword">
                <el-input v-model="userInfo.repassword" style="width: 300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUserInfo">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <user-info />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <job-info />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailByIdApi } from '@/api/employees'
import UserInfo from './components/user-info.vue'
import JobInfo from './components/job-info.vue'
export default {
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    const checkRepassword = (rule, value, callback) => {
      if (value !== this.userInfo.password) {
        return callback(new Error('两次输入的密码不一致'))
      }
      callback()
    }
    return {
      id: this.$route.params.id, // 从路由地址中获取用户的id
      userInfo: {
        username: '',
        password: '',
        repassword: ''
      },
      userInfoRules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度6-9位', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度6-9位', trigger: 'blur' },
          { validator: checkRepassword, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserDetail()
  },
  methods: {
    // 获取员工基本详情数据
    async getUserDetail() {
      const res = await getUserDetailById(this.id)
      this.userInfo.username = res.username
    },
    // 保存员工的基本信息
    saveUserInfo() {
      this.$refs.userInfoRef.validate(async isOk => {
        if (!isOk) return false
        await saveUserDetailByIdApi(this.userInfo)
        this.$message.success('保存成功')
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
